# -*- coding: utf-8 -*-
from five import grok
from zope.i18n import translate
from zope.i18nmessageid import MessageFactory
from zope.interface import implements
from Products.ATVocabularyManager.config import TOOL_NAME as ATVOCABULARYTOOL
from zope.schema.interfaces import IContextSourceBinder
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
from zope.site.hooks import getSite
from Acquisition import aq_get
from Products.CMFCore.utils import getToolByName
from plone.memoize.instance import memoize
from Acquisition import aq_parent
_ = MessageFactory('plone')
class CategoriasVocab(object):
    implements(IVocabularyFactory)

    def __call__(self, context):
        return self.generaVocab()

    @memoize
    def generaVocab(self):
        terms = []
        site = getSite()
        terminos=[]


        wcat  = getToolByName(site, 'portal_catalog', None)

        cata=wcat(object_provides="Products.Archetypes.interfaces.vocabulary.IVocabulary")

        for brain in set(cata):
            termTMP=brain.Title.decode('utf8')
            term=termTMP.encode('utf8','ignore')

            barinToken=brain.UID


            if term not in terminos:
                terminos.append(term)
                #barinToken=brain.UID+"re"
                #term="%s(%s)"%(term,aq_parent(brain.getObject()).title)

                if brain.id!="categorias_vocab":
                    terms.append(SimpleTerm(
                        token=barinToken,
                        value=brain.UID+':'+term,
                        title=term)
                    )



        #atvm = getToolByName(site, ATVOCABULARYTOOL)
        #vocabulario = atvm.getVocabularyByName('categorias_vocab')
        #lista=vocabulario.getDisplayList("tree")



        """
        for flaga in range(0,len(lista)):
            flag=flaga-1
            termTMP=lista.values()[flag].decode('utf8')
            term=termTMP.encode('utf8','ignore')
            terms.append(SimpleTerm(
                value=lista.keys()[flag]+':'+term,
                token=lista.keys()[flag],
                title=term)
            )

            if flag==len(lista.keys()):
                break;
            ##lista=vocabulario.listVocabularies()
            """

        return SimpleVocabulary(terms)




class GroupMembers(object):
    """Context source binder to provide a vocabulary of users in a given
    group.
    """
    implements(IVocabularyFactory)

    def __init__(self, group_name):
        self.group_name = group_name

    def __call__(self, context):
        acl_users = getToolByName(context, 'acl_users')
        group = acl_users.getGroupById(self.group_name)
        terms = []

        if group is not None:
            for member_id in group.getMemberIds():
                user = acl_users.getUserById(member_id)
                if user is not None:
                    member_name = user.getProperty('fullname') or member_id
                    terms.append(SimpleVocabulary.createTerm(member_id, str(member_id), member_name))

        return SimpleVocabulary(terms)


GroupMembersVocabFactory = GroupMembers("Staff")
CategoriasVocabFactory = CategoriasVocab()